package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.EmployeeUpdatePasswordDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployeeService employeeService;
    private final JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO 包含登录必要信息的员工登录DTO
     * @return 返回登录成功后的员工信息及JWT令牌
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        // 登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return 成功退出返回结果
     */
    @PostMapping("/logout")
    @ApiOperation(value = "员工退出")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     *
     * @param employeeDTO 包含员工信息的DTO
     * @return 添加成功返回结果
     */
    @PostMapping
    @ApiOperation("新增员工")
    public Result addEmp(@RequestBody EmployeeDTO employeeDTO) {
        log.info("新增员工: {}", employeeDTO);
        employeeService.addEmp(employeeDTO);
        return Result.success();
    }

    /**
     * 分页查询所有员工信息
     *
     * @param employeePageQueryDTO 包含分页查询条件的DTO
     * @return 返回分页查询结果
     */
    @GetMapping("/page")
    @ApiOperation("分页查询所有员工信息")
    public Result<PageResult> selAllByPage(EmployeePageQueryDTO employeePageQueryDTO) {
        log.info("分页查询所有员工信息 {}", employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用、禁用员工账号
     *
     * @param status 员工账号状态（启用或禁用）
     * @param id     员工ID
     * @return 更新成功返回结果
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用、禁用员工账号")
    public Result empStatus(@PathVariable Integer status, Long id) {
        employeeService.updateEmpStatus(status, id);
        return Result.success();
    }

    /**
     * 根据id查询员工信息
     *
     * @param id 员工ID
     * @return 返回员工信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询员工信息")
    public Result<Employee> selEmpById(@PathVariable Long id) {
        Employee employee = employeeService.selEmpById(id);
        return Result.success(employee);
    }

    /**
     * 编辑员工信息
     *
     * @param employeeDTO 包含员工信息的DTO
     * @return 更新成功返回结果
     */
    @PutMapping
    @ApiOperation("编辑员工信息")
    public Result updateEmp(@RequestBody EmployeeDTO employeeDTO) {
        employeeService.updateEmpData(employeeDTO);
        return Result.success();
    }

    /**
     * 修改员工密码
     *
     * @param employeeUpdatePasswordDTO 包含员工ID和新密码的DTO
     * @return 修改成功返回结果，失败返回错误信息
     */
    @PutMapping("editPassword")
    @ApiOperation("修改员工密码")
    public Result updateEmpPassword(@RequestBody EmployeeUpdatePasswordDTO employeeUpdatePasswordDTO) {
        if (employeeService.updateEmpPassword(employeeUpdatePasswordDTO))
            return Result.success();
        else return Result.error("密码错误!");
    }

}
